//引入工具类
const util = require('utils/util');
// 加载配置文件
const config = require('utils/config');

App({
    // 每次打开都需要验证是否绑定了账号，防止解绑还能查看，故使用onShow
    onShow: function(options) {
        this.checkBind(options.path);
    },
    checkBind: function(curPage) {
        var _this = this
        // 微信登录获取openid
        wx.login({
            success: res => {
                // wx.showLoading({
                //     title: '加载中...'
                // })
                util.ajax({
                    url: `${config.httpUrl}log/loginWxServer`,
                    method: 'get',
                    data: {
                        code: res.code
                    },
                    callback: function(error, wxData) {
                        // wx.hideLoading();
                        wx.setStorageSync('openid', wxData.data.openid)

                        // 检测是否授权、绑定
                        wx.getSetting({
                            success: settingData => {
                                if (settingData.authSetting['scope.userInfo']) {
                                    wx.getUserInfo({
                                        success: userData => {
                                            _this.globalData.userInfo = userData.userInfo
                                        }
                                    })
                                    util.ajax({
                                        url: `${config.httpUrl}log/binding`,
                                        method: 'get',
                                        data: {
                                            wxAccount: wxData.data.openid
                                        },
                                        callback: function(error, data) {
                                            if (data.data.data) {
                                                // if (curPage == 'pages/index/index') return;
                                                // let url = '/pages/index/index'
                                                // wx.reLaunch({
                                                //     url
                                                // });
                                            } else {
                                                if (curPage == 'pages/login/login') return;
                                                let url = '/pages/login/login'
                                                wx.reLaunch({
                                                    url
                                                });
                                            }
                                        }
                                    })
                                } else {
                                    if (curPage == 'pages/login/login') return;
                                    let url = '/pages/login/login'
                                    wx.reLaunch({
                                        url
                                    });
                                }
                            }
                        })
                    }
                })
            }
        })
        console.log('app onShow');
    },
    // 请求api带上openid
    ajax(options) {
        var openid = wx.getStorageSync('openid')
        options.data = Object.assign({
            wxAccount: openid
        }, options.data)
        return util.ajax(options)
    },
    globalData: {
        userInfo: null,
        roleList: []
    }
})